package com.lcy.web.generator;

import com.jfinal.plugin.activerecord.generator.BaseModelGenerator;
import com.jfinal.plugin.activerecord.generator.TableMeta;

/**
 * Created by loucunyin on 2016/12/5 0005.
 * Base Model 中输出表名，以及表名的 get 方法，更改父类为自定义的 BaseModel
 */
public class MyBaseModelGenerator extends BaseModelGenerator {
    private String baseModelPackage = "import %s.mine.BaseModel;%n%n";
    public MyBaseModelGenerator(String baseModelPackageName, String baseModelOutputDir) {
        super(baseModelPackageName, baseModelOutputDir);
        importTemplate = "import com.jfinal.plugin.activerecord.IBean;%n";
        classDefineTemplate = "/**%n * Generated by loucunyin.%n */%npublic abstract class %s<M extends %s<M>> extends BaseModel<M> implements IBean {%n%n";
    }

    @Override
    protected void genClassDefine(TableMeta tableMeta, StringBuilder ret) {
        super.genClassDefine(tableMeta, ret);
        genTableName(tableMeta, ret);

        genGetTableName(tableMeta,ret);
    }

    protected void genTableName(TableMeta tableMeta, StringBuilder ret) {
        ret.append("\tpublic static final String TABLE_NAME = \"")
                .append(tableMeta.name)
                .append("\" ;\n\n");
    }

    protected void genGetTableName(TableMeta tableMeta, StringBuilder ret){
        String getTable = "\tpublic String getTableName() {\n\t\treturn TABLE_NAME;\n\t}\n\n";
        ret.append(getTable);
    }

    @Override
    protected void genImport(StringBuilder ret) {
        super.genImport(ret);
        ret.append(String.format(baseModelPackage,baseModelPackageName));
    }
}
